var schedule = require('node-schedule');
var querystring = require('querystring');
var tuling = require('./tuling');
var config = require('./config');
var utils = require('./utils');

var rule = config.rule();

config.users().forEach(function (element) {
    var userData = element;
    var tweet = schedule.scheduleJob(rule, function () {

        console.log("start tweet schedule.");

        //在callback里面
        tuling.tweetMsg("", function (data) {

            console.log("callback-data:" + data);

            var _msg = data ? data : "[" + parseInt(Math.random() * 134 + 1) + "]";

            var postData = querystring.stringify({
                user_code: userData.userCode,
                user: userData.userId,
                content: _msg
            });

            var options = {
                host: "www.oschina.net",
                port: 80,
                path: 'http://www.oschina.net/action/apiv2/tweet',
                method: 'POST',
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                    'Content-Length': postData.length,
                    "User-Agent": "Mozilla/5.0 (Linux; U; Android 6.0.1; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
                    "X-Requested-With": "XMLHttpRequest",
                    "Cookie": userData.userCookie
                }
            };

            utils.request(postData, options, function (chunk) {
                console.log('result: ' + chunk);
            });

        });

        console.log("tweet finished.");

    });
}, this);


